{% extends "contest/base.html" %}
{% load full_time %}
{% block contest_page_title%}裁判名单 - {% endblock %}
{% block contest_page_body %}
    <h4>
        裁判名单管理
    </h4>
    <hr>
    <form id="form_search" class="form-inline" action="{% url 'contest_mgr_find_participants' contest.id %}" method="post">
        <div class="input-group">
            <div class="input-group-addon">搜索用户（支持正则表达式）</div>
            <input type="text" name="user_reg" class="form-control" size="50"/>
            <div class="input-group-btn">
                <button id="add_new_user_search" type="submit" class="btn btn-primary">搜索</button>
            </div>
        </div>
    </form><br>
    <span class="glyphicon glyphicon-warning-sign"></span>&nbsp;请注意，用于MYSQL的正则表达式查询语句和标准的正则有些不同，<a href="http://dev.mysql.com/doc/refman/5.6/en/regexp.html">参见详情</a>
    <br><br>
    <form id="from_add_new_user" action="{% url 'contest_mgr_add_referees' contest.id %}" method="post">
        <div id="user_list" style="overflow: scroll; max-height: 300px; display: none; border: 1px solid #999; padding: 20px;">

        </div>
        <div id="add_new_user_layout" style="display: none;">
            <hr>
            <button type="submit" class="btn btn-success">添加</button>
        </div>
    </form>
    <hr>
    <form id="from_delete_user" action="{% url 'contest_mgr_remove_referees' contest.id %}" method="post">
        <table class="table table-striped table-bordered">
            <thead>
            <tr>
                <td>ID</td>
                <td>用户名</td>
                <td>真实姓名</td>
                <td><input class="bm-delete-all" type="checkbox"> 移除？</td>
            </tr>
            </thead>
            <tbody>
            {% for user in referees %}
            <tr>
                <td>{{ user.id }}</td>
                <td>{% if user.sex == 0 %}<span style="color:red;">{{ user.nickname | default:"" }}</span>{% else %}{{ user.nickname | default:"" }}{% endif %}</td>
                <td>{{ user.realname | default:"" }}</td>
                <td>
                    <input class="bm-delete" type="checkbox" name="user_id" value="{{ user.id }}"> 移除
                </td>
            </tr>
            {% empty %}
                <tr>
                    <td colspan="4">暂时没有给本次比赛分配可用裁判</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <button class="btn btn-danger">移除</button>
    </form>
{% endblock %}
{% block contest_page_script %}
    <script type="text/javascript">
        $(function () {
            $(".bm-delete-all").change(function () {
                $(".bm-delete").prop("checked", $(this).prop("checked"));
            });

            $("#form_search").submit(function () {
                $.GRestP({
                    responseType: "json",
                    callback: function (flag, entity) {
                        if(flag){
                            var $userList = $("#user_list");
                            $userList.show();
                            if(entity.data.length > 0) {
                                $userList.html("");  //clear
                                $userList.append("<input type='checkbox' class='bm-addnew-all' checked> 全选/全不选<br /><br />");
                                $(".bm-addnew-all").change(function () {
                                    $(".bm-addnew").prop("checked", $(this).prop("checked"));
                                });
                                for (var i = 0; i < entity.data.length; i++) {
                                    $userList.append(
                                        "<input class='bm-addnew' type='checkbox' name='user_id' value='" + entity.data[i].id + "' checked />&nbsp;"
                                        + entity.data[i].id + "&nbsp;(" + entity.data[i].nickname + "；" + entity.data[i].realname + ")<br />"
                                    )
                                }
                                $("#add_new_user_layout").show();
                            }else{
                                $userList.html('查无记录');
                                $("#add_new_user_layout").hide();
                            }
                        }else{
                            $.AlertBox({body: entity.msg, color: "danger", title: "查询失败"}).show();
                        }
                    }
                }).submit_form(this);
                return false;
            });
            $("#from_add_new_user").submit(function () {
                $.GRestP({
                    responseType: "json",
                    callback: function (flag, entity) {
                        if(flag){
                            $.AlertBox({
                                body: "裁判添加完毕",
                                color: "success",
                                title: "操作成功",
                                callback: function () {
                                    $.ModalBox({}).hide();
                                    window.location.reload();
                                }
                            }).show();
                        }else{
                            $.AlertBox({body: entity.msg, color: "danger", title: "操作失败"}).show();
                        }
                    }
                }).submit_form(this);
                return false;
            });
            $("#from_delete_user").submit(function () {
                $.GRestP({
                    responseType: "json",
                    callback: function (flag, entity) {
                        if(flag){
                            $.AlertBox({
                                body: "用户移除成功",
                                color: "success",
                                title: "操作成功",
                                callback: function () {
                                    $.ModalBox({}).hide();
                                    window.location.reload();
                                }
                            }).show();
                        }else{
                            $.AlertBox({body: entity.msg, color: "danger", title: "操作失败"}).show();
                        }
                    }
                }).submit_form(this);
                return false;
            });
        });
    </script>
{% endblock %}